<?php
namespace WebApper\Shortcode;

/*
 * [paymentterms]
 *
 */
 class PaymentTerms extends \WebApper\Shortcode {
	
    /**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'payment_terms';

    /**
     * Initialize the Shortcode Class
     *
     */
    public function __construct() {
		add_shortcode( $this->shortcode, array($this, 'shortcode') );
		add_action( 'wp_ajax_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );
		add_action( 'wp_ajax_nopriv_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );	   	
	}

    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
	 
    public function shortcode( $atts ) {

		global $wpdb, $webapper;
	$post_id = $_GET['postID'];  // Get the post ID
	$principalAmount = trim($_POST['principalAmount']);
	$trialAmount = trim($_POST['trialAmount']);	
	$trialLength = trim($_POST['trialLength']);	
	$termLength = trim($_POST['termLength']);	
	$billing = trim($_POST['billing']);
	$billingDate = trim($_POST['billingDate']);
	$user_payment_master_id = $_POST['hidden_user_id'];
	
	$get_post_meta      = get_post_meta($post_id);
	$email_address 		= $get_post_meta['email-primary'][0];
	$exist_user_id      = email_exists($email_address);
	
		// Save Payment information
		if (isset($_POST['saveTrialPeriod'])){ 
		
			if($_POST['hidden_user_id']==''){ // payment information exist for this user
				// Insert
				$result = $wpdb->insert( 
					$webapper->tables->user_payment_master, 
					array( 
						 'user_id'                  => $exist_user_id,
						 'principal_amount'         => $principalAmount,
						 'trail_amount'             => $trialAmount,
						 'trail_length'             => $trialLength,
						 'term_length'              => $termLength,
						 'billing_frequency'        => $billing,
						 'billing_date'             => $billingDate
					), 
					array('%d','%s','%s','%s','%s','%s','%s') 
				);
				$this->user_payment_master_id = $wpdb->insert_id;
			 }
				else{	
	        $result = $wpdb->update( 
				$webapper->tables->user_payment_master, 
				array( 
					 'user_id'                  => $exist_user_id,
					 'principal_amount'         => $principalAmount,
					 'trail_amount'             => $trialAmount,
					 'trail_length'             => $trialLength,
					 'term_length'              => $termLength,
					 'billing_frequency'        => $billing,
					 'billing_date'             => $billingDate
				), 
				array( 'user_id' => $exist_user_id ), 
				array( '%s','%s','%s','%s','%s','%s'), 
				array( '%d' ) 
			);	
			       }
			
		}
		
		//--------------------------------  FETCHING VALUES FROM PAYMENT MASTER TABLE -------------------------------------
	        $payment_master         = web_apper_get_payment_master($exist_user_id);
			$user_payment_master_id = $payment_master ->user_payment_master_id;
	        $user_id                = $payment_master ->user_id;
	        $principalAmount    	= $payment_master ->principal_amount;
			$trialAmount         	= $payment_master ->trail_amount;
			$trialLength        	= $payment_master ->trail_length;
			$termLength   	        = $payment_master ->term_length;
			$billing   	            = $payment_master ->billing_frequency;
			$billingDate   	        = $payment_master ->billing_date;
			
//---------------------------------------------END FETCHING VALUES--------------------------------------
	?>

<div id="paymentTerms" style="display:none;">
  <h3> Earnest Trial Period</h3>
  <form class="form-horizontal" action="" method="post" name="earnestTrialPeriod">
    <div class="control-group">
      <input type="hidden" name="hidden_user_id" value="<?php echo $user_payment_master_id; ?>">
      <label class="control-label" for="Principal Amount">Principal Amount:</label>
      <div class="controls">
        <input type="text" id="principalAmount" required="required" name="principalAmount" placeholder="Principal Amount" value="<?php  echo $principalAmount; ?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Principal Amount">Trial Amount:</label>
      <div class="controls">
        <input type="text" id="trialAmount" required="required" name="trialAmount" placeholder="Trial Amount" value="<?php  echo $trialAmount;?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" >Trial Length</label>
      <div class="controls">
        <select name="trialLength" id="trialLength" class="selectpicker" data-live-search="true" data-size="5">
          <?php 
			for($i=1; $i<=6; $i++){
				if($i == $trialLength){
				  echo "<option value=".$i." selected='selected'>".$i." Months</option>";
				}
				else{
				  echo "<option value=".$i.">".$i." Months</option>";
				}
			}
			?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Trial Payment">Trial Payment:</label>
      <div class="controls">
        <label class="control-label" for="Trial Payment">$999.00</label>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Principle Balance">Principle Balance:</label>
      <div class="controls">
        <label class="control-label" for="Principle Balance">$90000.00</label>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" >Term Length</label>
      <div class="controls">
        <select name="termLength"  id="termLength" class="selectpicker" data-live-search="true" data-size="5">
          <?php 
			 $termLengthArray =array('12','24','36'); 
				foreach($termLengthArray as $termlength){
					if($termlength == $termLength){
				   echo "<option value=".$termlength." selected='selected'>".$termlength." Months </option>";
				}
				else{
				    echo "<option value=".$termlength.">".$termlength." Months</option>";
				}
				}
			?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" >Billing Frequency</label>
      <div class="controls">
        <select name="billing" class="selectpicker" id="billing"  data-live-search="true" data-size="5" >
          <?php
				$billingArray =array('Bi-monthly','Monthly'); 
				foreach($billingArray as $billingfreq){
					if($billingfreq == $billing){
				   echo "<option value=".$billingfreq." selected='selected'>".$billingfreq." </option>";
				}
				else{
				    echo "<option value=".$billingfreq.">".$billingfreq." </option>";
				}
				}
            ?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Billing Date">Billing Date:</label>
      <div class="controls">
        <input type="text" id="billingDate" required="required" name="billingDate" value="<?php echo $billingDate; ?>" >
      </div>
    </div>
    <div class="control-group">
      <div class="controls">
        <button type="submit" class="btn btn-primary" name="saveTrialPeriod">Save</button>
      </div>
    </div>
  </form>
</div>
<?php	}		
}
$initialize = new PaymentTerms(); 

?>
